<?php

namespace app\home\home;
use app\admin\controller\Admin;
use app\home\model\Bindbank;
use app\home\model\Gift;
use app\home\model\User;
use think\Controller;
use think\Db;
use think\Loader;
use think\Request;
use app\common\model\AdminAnnex as AnnexModel;
class Orders extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {
        $data = $request->param();
        isset($data['status']) ? $data['status'] : $data['status'] = 1;
        $data['listrow'] = 6;
        $gift = new Bindbank();
        $res = $gift->lists($data);
        if ($res){
            return apistatus(1,'列表获取成功',$res);
        }else{
            return apistatus(0,'无数据');
        }

    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function createOrder(Request $request)
    {
        !request()->isPost() && datamsg(0,'非法操作');
        $data   = input();
        //验证用户
        self::checktoken($data['id']);
        //验证提交的值
        $price = db('home_charge')->where(['id'=>$data['chargeid']])->field('price,count')->find();
        if (!$price['price']){
            return apistatus(0,'价格错误');
        }
        $res=[
            'uid'       => $data['id'],
            'ordersn'       => 'T'.date('YmdHis').rand(10000,99999),
            'price'         => $data['price'],
            'chargeid'      => $data['chargeid'],
            'paytype'       => $data['paytype'],
            'count'         => $price['count'],
            'create_time'   => time(),
        ];
        $id = \db('home_orders')->insertGetId($res);
        if ($id){
            return apistatus(1,'订单创建成功',$id);
        }else{
            return apistatus(0,'订单创建失败');
        }
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //

    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete(Request $request)
    {
        $data = $request->param();
        if (empty($data['id'])){
            return apistatus('0','请提交银行卡id');
        }
        $res = \db('home_bindbank')->where('id',$data['id'])->update(['status'=>2,'delete_time'=>time()]);
        if ($res){
            return apistatus(1,'删除成功');
        }else{
            return apistatus(0,'删除失败请重试');
        }
    }
    /**
     * 查看我的绑定银行

     */
    public  function mybankList(Request $request)
    {
        $data = $request->param();
        $validate = validate('bindbanks');
        $res = $validate->scene('getbanklist')->check($data);
        if (!$res){
            return apistatus(0,$validate->getError());
        }
        $where['uid'] = $data['id'];
        $where['status'] = 1;
        $banklist = model('bindbank')->where($where)->order('create_time desc')->paginate();
        if (!empty($banklist)){
            return apistatus(1,'列表获取成功',$banklist);
        }else{
            return apistatus(0,'无数据');
        }
    }





}
